package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.entity.UserFeedback;
import com.scuec.groupfive.mapper.UserFeedbackMapper;
import com.scuec.groupfive.service.UserFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserFeedbackServiceImpl implements UserFeedbackService {

    @Autowired
    private UserFeedbackMapper userFeedbackMapper;

    @Override
    public Map<String, Object> getFeedbackList(int pageNum, int pageSize, String timeRange, String status, String feedbackType, String searchType, String keyword) {
        Map<String, Object> result = new HashMap<>();

        // 计算分页偏移量
        int offset = (pageNum - 1) * pageSize;

        // 构建查询条件
        Map<String, Object> params = new HashMap<>();
        params.put("offset", offset);
        params.put("pageSize", pageSize);

        // 时间范围筛选
        if (!StringUtils.isEmpty(timeRange)) {
            Date now = new Date();
            long currentTime = now.getTime();

            switch (timeRange) {
                case "today":
                    params.put("timeCondition", "AND created_at >= DATE_SUB(NOW(), INTERVAL 1 DAY)");
                    break;
                case "3days":
                    params.put("timeCondition", "AND created_at >= DATE_SUB(NOW(), INTERVAL 3 DAY)");
                    break;
                case "week":
                    params.put("timeCondition", "AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)");
                    break;
                case "overweek":
                    params.put("timeCondition", "AND created_at < DATE_SUB(NOW(), INTERVAL 7 DAY)");
                    break;
            }
        }

        // 状态筛选
        if (!StringUtils.isEmpty(status)) {
            params.put("status", status);
        }

        // 反馈类型筛选
        if (!StringUtils.isEmpty(feedbackType)) {
            params.put("feedbackType", feedbackType);
        }

        // 搜索功能
        if (!StringUtils.isEmpty(searchType) && !StringUtils.isEmpty(keyword)) {
            switch (searchType) {
                case "content":
                    params.put("searchCondition", "AND (title LIKE CONCAT('%', #{keyword}, '%') OR content LIKE CONCAT('%', #{keyword}, '%'))");
                    break;
                case "userId":
                    params.put("searchCondition", "AND user_id = #{keyword}");
                    break;
                case "username":
                    params.put("searchCondition", "AND username LIKE CONCAT('%', #{keyword}, '%')");
                    break;
                case "feedbackId":
                    params.put("searchCondition", "AND feedback_id = #{keyword}");
                    break;
            }
            params.put("keyword", keyword);
        }

        // 获取分页数据
        List<UserFeedback> feedbackList = userFeedbackMapper.getFeedbackListByCondition(params);
        // 获取总记录数
        int total = userFeedbackMapper.getFeedbackCountByCondition(params);

        result.put("list", feedbackList);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        result.put("pages", (int) Math.ceil((double) total / pageSize));

        return result;
    }

    @Override
    public int getFeedbackCount() {
        return userFeedbackMapper.getFeedbackCount();
    }

    @Override
    public UserFeedback getFeedbackDetail(String id) {
        return userFeedbackMapper.getFeedbackById(id);
    }

    @Override
    public boolean deleteFeedback(String id) {
        int result = userFeedbackMapper.deleteFeedbackById(id);
        return result > 0;
    }

    @Override
    public List<UserFeedback> refreshFeedbackList() {
        return userFeedbackMapper.getAllFeedbacks();
    }
}